﻿using Sprout.Core.Paging;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sprout.Repository
{
    public static class DataService
    {
        #region 获取数据库连接
        /// <summary>
        /// 获取数据库连接
        /// </summary>
        /// <param name="dataSource"></param>
        /// <returns></returns>
        public static IDbConnection GetConnection(string dataSource = null)
        {
            return DapperProcess.Instance.GetConnection(dataSource);
        }
        #endregion

        #region 执行

        #region SQL执行

        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>受影响的行数</returns>
        public static int ExecuteSql(IDbConnection connection, string sql, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.ExecuteSql(connection, sql, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>受影响的行数</returns>
        public static async Task<int> ExecuteSqlAsync(IDbConnection connection, string sql, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.ExecuteSqlAsync(connection, sql, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 执行返回自增ID
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>自增ID</returns>
        public static long ExecuteReturnIdSql(IDbConnection connection, string sql, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.ExecuteReturnIdSql(connection, sql, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 执行返回自增ID
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>自增ID</returns>
        public static async Task<long> ExecuteReturnIdSqlAsync(IDbConnection connection, string sql, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.ExecuteReturnIdSqlAsync(connection, sql, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>受影响的行数</returns>
        public static int ExecuteSql(string sql, object parameters = null, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.ExecuteSql(sql, parameters, dataSource, commandTimeout, commandType);
        }

        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>受影响的行数</returns>
        public static async Task<int> ExecuteSqlAsync(string sql, object parameters = null, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.ExecuteSqlAsync(sql, parameters, dataSource, commandTimeout, commandType);
        }

        /// <summary>
        /// 执行返回自增ID
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>自增ID</returns>
        public static long ExecuteReturnIdSql(string sql, object parameters = null, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.ExecuteReturnIdSql(sql, parameters, dataSource, commandTimeout, commandType);
        }

        /// <summary>
        /// 执行返回自增ID
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>自增ID</returns>
        public static async Task<long> ExecuteReturnIdSqlAsync(string sql, object parameters = null, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.ExecuteReturnIdSqlAsync(sql, parameters, dataSource, commandTimeout, commandType);
        }

        #endregion

        #region 模板执行

        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>受影响的行数</returns>
        public static int Execute(IDbConnection connection, string statementId, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.Execute(connection, statementId, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>受影响的行数</returns>
        public static async Task<int> ExecuteAsync(IDbConnection connection, string statementId, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.ExecuteAsync(connection, statementId, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 执行返回自增ID
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>自增ID</returns>
        public static long ExecuteReturnId(IDbConnection connection, string statementId, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.ExecuteReturnId(connection, statementId, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 执行返回自增ID
        /// </summary>
        /// <param name="connection">数据库连接</param>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>自增ID</returns>
        public static async Task<long> ExecuteReturnIdAsync(IDbConnection connection, string statementId, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.ExecuteReturnIdAsync(connection, statementId, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>受影响的行数</returns>
        public static int Execute(string statementId, object parameters = null, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.Execute(statementId, parameters, dataSource, commandTimeout, commandType);
        }

        /// <summary>
        /// 执行
        /// </summary>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>受影响的行数</returns>
        public static async Task<int> ExecuteAsync(string statementId, object parameters = null, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.ExecuteAsync(statementId, parameters, dataSource, commandTimeout, commandType);
        }

        /// <summary>
        /// 执行返回自增ID
        /// </summary>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>自增ID</returns>
        public static long ExecuteReturnId(string statementId, object parameters = null, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.ExecuteReturnId(statementId, parameters, dataSource, commandTimeout, commandType);
        }

        /// <summary>
        /// 执行返回自增ID
        /// </summary>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>自增ID</returns>
        public static async Task<long> ExecuteReturnIdAsync(string statementId, object parameters = null, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.ExecuteReturnIdAsync(statementId, parameters, dataSource, commandTimeout, commandType);
        }

        #endregion

        #endregion

        #region 查询

        #region SQL查询

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="connection">数据库连接</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>数据集合</returns>
        public static IList<T> QuerySql<T>(IDbConnection connection, string sql, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.QuerySql<T>(connection, sql, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="connection">数据库连接</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>数据集合</returns>
        public static async Task<IList<T>> QuerySqlAsync<T>(IDbConnection connection, string sql, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.QuerySqlAsync<T>(connection, sql, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="connection">数据库连接</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>实体</returns>
        public static T QueryFirstSql<T>(IDbConnection connection, string sql, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.QueryFirstSql<T>(connection, sql, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="connection">数据库连接</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>实体</returns>
        public static async Task<T> QueryFirstSqlAsync<T>(IDbConnection connection, string sql, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.QueryFirstSqlAsync<T>(connection, sql, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>数据集合</returns>
        public static IList<T> QuerySql<T>(string sql, object parameters = null, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.QuerySql<T>(sql, parameters, dataSource, commandTimeout, commandType);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>数据集合</returns>
        public static async Task<IList<T>> QuerySqlAsync<T>(string sql, object parameters = null, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.QuerySqlAsync<T>(sql, parameters, dataSource, commandTimeout, commandType);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>实体</returns>
        public static T QueryFirstSql<T>(string sql, object parameters = null, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.QueryFirstSql<T>(sql, parameters, dataSource, commandTimeout, commandType);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>实体</returns>
        public static async Task<T> QueryFirstSqlAsync<T>(string sql, object parameters = null, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.QueryFirstSqlAsync<T>(sql, parameters, dataSource, commandTimeout, commandType);
        }

        #endregion

        #region 模板查询

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="connection">数据库连接</param>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>数据集合</returns>
        public static IList<T> Query<T>(IDbConnection connection, string statementId, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.Query<T>(connection, statementId, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="connection">数据库连接</param>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>数据集合</returns>
        public static async Task<IList<T>> QueryAsync<T>(IDbConnection connection, string statementId, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.QueryAsync<T>(connection, statementId, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="connection">数据库连接</param>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>数据集合</returns>
        public static T QueryFirst<T>(IDbConnection connection, string statementId, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.QueryFirst<T>(connection, statementId, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="connection">数据库连接</param>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>数据集合</returns>
        public static async Task<T> QueryFirstAsync<T>(IDbConnection connection, string statementId, object parameters = null, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.QueryFirstAsync<T>(connection, statementId, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>数据集合</returns>
        public static IList<T> Query<T>(string statementId, object parameters = null, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.Query<T>(statementId, parameters, dataSource, commandTimeout, commandType);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>数据集合</returns>
        public static async Task<IList<T>> QueryAsync<T>(string statementId, object parameters = null, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.QueryAsync<T>(statementId, parameters, dataSource, commandTimeout, commandType);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="statementId">SQL语句ID</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>实体</returns>
        public static T QueryFirst<T>(string statementId, object parameters = null, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.QueryFirst<T>(statementId, parameters, dataSource, commandTimeout, commandType);
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <typeparam name="T">泛型</typeparam>
        /// <param name="statementId">SQL语句ID</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>实体</returns>
        public static async Task<T> QueryFirstAsync<T>(string statementId, object parameters = null, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.QueryFirstAsync<T>(statementId, parameters, dataSource, commandTimeout, commandType);
        }

        #endregion

        #endregion

        #region 分页查询

        #region SQL分页

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connection">数据库连接</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="isCount">是否获取总数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>分页数据结果</returns>
        public static PagedList<T> QueryPagingSql<T>(IDbConnection connection, string sql, PageParameter parameters, bool isCount, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.QueryPagingSql<T>(connection, sql, parameters, isCount, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connection">数据库连接</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="isCount">是否获取总数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>分页数据结果</returns>
        public static async Task<PagedList<T>> QueryPagingSqlAsync<T>(IDbConnection connection, string sql, PageParameter parameters, bool isCount, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.QueryPagingSqlAsync<T>(connection, sql, parameters, isCount, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connection">数据库连接</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="countSql">查询总数SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>分页数据结果</returns>
        public static PagedList<T> QueryPagingSql<T>(IDbConnection connection, string sql, string countSql, PageParameter parameters, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.QueryPagingSql<T>(connection, sql, countSql, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connection">数据库连接</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="countSql">查询总数SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>分页数据结果</returns>
        public static async Task<PagedList<T>> QueryPagingSqlAsync<T>(IDbConnection connection, string sql, string countSql, PageParameter parameters, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.QueryPagingSqlAsync<T>(connection, sql, countSql, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="isCount">是否获取总数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>分页数据结果</returns>
        public static PagedList<T> QueryPagingSql<T>(string sql, PageParameter parameters, bool isCount, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.QueryPagingSql<T>(sql, parameters, isCount, dataSource, commandTimeout, commandType);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="isCount">是否获取总数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>分页数据结果</returns>
        public static async Task<PagedList<T>> QueryPagingSqlAsync<T>(string sql, PageParameter parameters, bool isCount, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.QueryPagingSqlAsync<T>(sql, parameters, isCount, dataSource, commandTimeout, commandType);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql">SQL语句</param>
        /// <param name="countSql">查询总数SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>分页数据结果</returns>
        public static PagedList<T> QueryPagingSql<T>(string sql, string countSql, PageParameter parameters, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.QueryPagingSql<T>(sql, countSql, parameters, dataSource, commandTimeout, commandType);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql">SQL语句</param>
        /// <param name="countSql">查询总数SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>分页数据结果</returns>
        public static async Task<PagedList<T>> QueryPagingSqlAsync<T>(string sql, string countSql, PageParameter parameters, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.QueryPagingSqlAsync<T>(sql, countSql, parameters, dataSource, commandTimeout, commandType);
        }

        #endregion

        #region 模板分页

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connection">数据库连接</param>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="isCount">是否获取总数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>分页数据结果</returns>
        public static PagedList<T> QueryPaging<T>(IDbConnection connection, string statementId, PageParameter parameters, bool isCount, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.QueryPaging<T>(connection, statementId, parameters, isCount, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connection">数据库连接</param>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="isCount">是否获取总数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>分页数据结果</returns>
        public static async Task<PagedList<T>> QueryPagingAsync<T>(IDbConnection connection, string statementId, PageParameter parameters, bool isCount, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.QueryPagingAsync<T>(connection, statementId, parameters, isCount, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connection">数据库连接</param>
        /// <param name="statementId">statementId</param>
        /// <param name="countStatementId">查询总数statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>分页数据结果</returns>
        public static PagedList<T> QueryPaging<T>(IDbConnection connection, string statementId, string countStatementId, PageParameter parameters, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.QueryPaging<T>(connection, statementId, countStatementId, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connection">数据库连接</param>
        /// <param name="statementId">statementId</param>
        /// <param name="countStatementId">查询总数statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="transaction">事务</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>分页数据结果</returns>
        public static async Task<PagedList<T>> QueryPagingAsync<T>(IDbConnection connection, string statementId, string countStatementId, PageParameter parameters, IDbTransaction transaction = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.QueryPagingAsync<T>(connection, statementId, countStatementId, parameters, transaction, commandTimeout, commandType);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="isCount">是否获取总数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>分页数据结果</returns>
        public static PagedList<T> QueryPaging<T>(string statementId, PageParameter parameters, bool isCount, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.QueryPaging<T>(statementId, parameters, isCount, dataSource, commandTimeout, commandType);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="isCount">是否获取总数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>分页数据结果</returns>
        public static async Task<PagedList<T>> QueryPagingAsync<T>(string statementId, PageParameter parameters, bool isCount, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.QueryPagingAsync<T>(statementId, parameters, isCount, dataSource, commandTimeout, commandType);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="statementId">statementId</param>
        /// <param name="countStatementId">查询总数statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>分页数据结果</returns>
        public static PagedList<T> QueryPaging<T>(string statementId, string countStatementId, PageParameter parameters, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return DapperProcess.Instance.QueryPaging<T>(statementId, countStatementId, parameters, dataSource, commandTimeout, commandType);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="statementId">statementId</param>
        /// <param name="countStatementId">查询总数statementId</param>
        /// <param name="parameters">参数</param>
        /// <param name="dataSource">数据源</param>
        /// <param name="commandTimeout">超时时间（秒）</param>
        /// <param name="commandType">执行类型</param>
        /// <returns>分页数据结果</returns>
        public static async Task<PagedList<T>> QueryPagingAsync<T>(string statementId, string countStatementId, PageParameter parameters, string dataSource = null, int? commandTimeout = null, CommandType? commandType = null)
        {
            return await DapperProcess.Instance.QueryPagingAsync<T>(statementId, countStatementId, parameters, dataSource, commandTimeout, commandType);
        }

        #endregion

        #endregion


        #region 获取执行SQL
        /// <summary>
        /// 获取运行时SQL
        /// </summary>
        /// <param name="sql">SQL语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>SQL语句</returns>
        public static string GetRuningSqlBySql(string sql, object parameters)
        {
            return DapperProcess.Instance.GetRuningSqlBySql(sql, parameters);
        }

        /// <summary>
        /// 获取运行时SQL
        /// </summary>
        /// <param name="statementId">statementId</param>
        /// <param name="parameters">参数</param>
        /// <returns>SQL语句</returns>
        public static string GetRuningSql(string statementId, object parameters)
        {
            return DapperProcess.Instance.GetRuningSql(statementId, parameters);
        }
        /// <summary>
        /// 获取运行时SQL
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="statementId"></param>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public static string GetRuningSql<T>(string statementId, object parameters)
            where T : IRepository
        {
            var interfaceType = typeof(T);
            var attributes = interfaceType.GetCustomAttributes(true);
            var repositoryAttribute = (RepositoryAttribute)attributes.FirstOrDefault(n => n is RepositoryAttribute);
            string sqlNameSpace = string.Empty;
            if (repositoryAttribute != null)
            {
                sqlNameSpace = string.IsNullOrEmpty(repositoryAttribute.SqlNameSpace) ? interfaceType.FullName : repositoryAttribute.SqlNameSpace;
            }
            statementId = $"{sqlNameSpace}.{statementId}";

            return GetRuningSql(statementId, parameters);
        }
        #endregion

    }
}
